package com.cloud.common.Swagger;

import org.springframework.util.CollectionUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

public class SwaggerConfigBuilder {
    private static final String HEADER="header";//@RequestHeader()
    private static final String QUERY="query";//@RequestParam()
    private static final String PATH="path";//@PathVariable()
    private static final String BODY="body";//@RequestBody()

    private List<ParameterBuilder> parameterBuilders=new ArrayList<>();
    private Boolean enable=true;
    private String basePackage;
    private String title;
    private String description;
    private String version;
    private Contact contact;
    private ApiInfo apiInfo;


    public SwaggerConfigBuilder() {
    }

    public SwaggerConfigBuilder(String basePackage,String title, String description, String version,Boolean enable) {
        this.basePackage=basePackage;
        this.title = title;
        this.description = description;
        this.version = version;
        this.enable=enable;
    }

    private Boolean getEnable() {
        return enable;
    }

    private void setEnable(Boolean enable) {
        this.enable = enable;
    }

    private Contact getContact() {
        return contact;
    }

    private void setContact(Contact contact) {
        this.contact = contact;
    }

    private List<ParameterBuilder> getParameterBuilders() {
        return parameterBuilders;
    }

    private void setParameterBuilders(List<ParameterBuilder> parameterBuilders) {
        this.parameterBuilders = parameterBuilders;
    }

    private String getBasePackage() {
        return basePackage;
    }

    private void setBasePackage(String basePackage) {
        this.basePackage = basePackage;
    }

    private String getTitle() {
        return title;
    }

    private void setTitle(String title) {
        this.title = title;
    }

    private String getDescription() {
        return description;
    }

    private void setDescription(String description) {
        this.description = description;
    }

    private String getVersion() {
        return version;
    }

    private void setVersion(String version) {
        this.version = version;
    }
    private ApiInfo getApiInfo() {
        return apiInfo;
    }

    private void setApiInfo(ApiInfo apiInfo) {
        this.apiInfo = apiInfo;
    }

    /**
     * 创建Swagger配置
     * @param title
     * @param description
     * @param version
     * @return
     */
    public static SwaggerConfigBuilder createBuilder(String basePackage,String title, String description, String version,Boolean enable){
        SwaggerConfigBuilder swaggerConfigBuilder=new SwaggerConfigBuilder(basePackage,title,description,version,enable);
        return swaggerConfigBuilder;
    }

    /**
     * 添加接口联系人
     * @param name
     * @param url
     * @param email
     * @return
     */
    public SwaggerConfigBuilder addContact(String name,String url, String email){
        this.contact=new Contact(name, url, email);
        return this;
    }
    /**
     * 给每一个接口添加统一的请求头，请求参数
     * @return
     */
    public SwaggerConfigBuilder addParameterBuilder(String name,String description){
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name(name).description(description).modelRef(new ModelRef("string")).parameterType(HEADER).required(true).build();
        this.parameterBuilders.add(tokenPar);
        return this;
    }
    /**
     * 给每一个接口添加统一的请求头，请求参数
     * @return
     */
    private SwaggerConfigBuilder addParameterBuilder(String name,String description,Boolean required){
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name(name).description(description).modelRef(new ModelRef("string")).parameterType(HEADER).required(required).build();
        this.parameterBuilders.add(tokenPar);
        return this;
    }
    /**
     * 给每一个接口添加统一的请求头，请求参数
     * @return
     */
    private SwaggerConfigBuilder addParameterBuilder(String name,String description,String parameterType,Boolean required){
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name(name).description(description).modelRef(new ModelRef("string")).parameterType(parameterType).required(required).build();
        this.parameterBuilders.add(tokenPar);
        return this;
    }
    private  ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(this.description)
                .contact(contact)
                .version(version)
                .build();

    }
    public Docket build(){
        List<Parameter> pars = new ArrayList<>();
        for (ParameterBuilder pa: parameterBuilders) {
            pars.add(pa.build());
        }
       return new Docket(DocumentationType.SWAGGER_2)
               .apiInfo(apiInfo())
               .enable(enable)
               .select()
               .apis(RequestHandlerSelectors.basePackage(basePackage))
               .paths(PathSelectors.any())
               .build()
               .globalOperationParameters(pars);
    }
}
